package com.tywj.lib.core.common.util

import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.LinearInterpolator
import com.github.florent37.viewanimator.AnimationBuilder
import com.github.florent37.viewanimator.ViewAnimator

/**
 * <pre>
 *     author : Taylor Zhang
 *     time   : 2017/9/25
 *     desc   : 动画操作工具类
 *     version: 1.0.0
 * </pre>
 */
object AnimationUtil {

    const val DURATION_CW = 400L
    const val DURATION_CCW = 400L
    const val DURATION_ALPHA_SHOW = 500L
    const val DURATION_ALPHA_HIDE = 150L
    const val DURATION_DIALOG_SHOW = 200L

    fun animate(view: View): AnimationBuilder = ViewAnimator.animate(view)

    fun rotateCw(view: View, duration: Long = DURATION_CW): ViewAnimator {
        view.clearAnimation()
        return animate(view)
            .duration(duration)
            .rotation(0f, 359f)
            .repeatCount(-1)
            .interpolator(LinearInterpolator())
            .start()
    }

    fun rotateCcw(view: View, duration: Long = DURATION_CCW): ViewAnimator {
        view.clearAnimation()
        return animate(view)
            .duration(duration)
            .rotation(359f, 0f)
            .repeatCount(-1)
            .interpolator(LinearInterpolator())
            .start()
    }

    fun alphaShow(view: View): ViewAnimator {
        view.clearAnimation()
        return animate(view)
            .duration(DURATION_ALPHA_SHOW)
            .alpha(0f, 1f)
            .interpolator(LinearInterpolator())
            .start()
    }

    fun alphaHide(view: View, duration: Long = DURATION_ALPHA_HIDE): ViewAnimator {
        view.clearAnimation()
        return animate(view)
            .duration(duration)
            .alpha(1f, 0f)
            .interpolator(LinearInterpolator())
            .start()
    }

    fun showDialog(view: View, duration: Long = DURATION_DIALOG_SHOW) {
        animate(view)
            .scale(0f, 1.1f)
            .duration(duration)
            .interpolator(AccelerateDecelerateInterpolator())
            .thenAnimate(view)
            .duration(80)
            .scale(1.1f, 1f)
            .interpolator(AccelerateDecelerateInterpolator())
            .start()
    }
}